<?php
class TagController extends  Controller{
	public function index(){
		
		$this->getSmarty();
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
		$click_count = isset ( $_GET ['order'] ) ? $_GET ['order'] : "";
		$tagId = isset ( $_GET ['tagid'] ) ?  $_GET ['tagid']  : "";

        $this->smarty->assign ( 'pagesets', Constant::tagHeader(""));

		$order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';

		$where = "";
		if (empty($tagId)){
			$where = "";
		}else {
			//根据标签id获取文章ID
			$result = $this->getPostIdByTagId($tagId);
			$where = "ID in ( ".rtrim($result, ",").") AND";
		}
		
		$this->getTemplatePosts($order, $where);
		$this->smarty->display("tag.tpl");
	}

	
	public function getPostIdByTagId($tagId){
		require_once SERVICE . DS . 'TagService.class.php';
		$tagService = new TagService( $this->getDB () );
		$result = $tagService->getPostIdByTagId($tagId);
		$postIds = "";
		foreach ($result as $k => $val) {
			$postIds .= $val->post_id . ",";
		}
		return $postIds;
	}
	
	
	/**
	 *
	 * 获取文章前30条数据
	 */


	public function getTemplatePosts($order, $where){
		require_once SERVICE . DS . 'IndexService.class.php';

		$indexService = new IndexService( $this->getDB () );
		
       	$result = 	$indexService->loadPosts4ListCategory(0, 30, $order, $where);
		
		$this->smarty->assign ( 'posts_html', $result );
	}
	/**
	 *
	 * 按照分类查看更多加载数据(文章)
	 */
	public function loadPosts(){
		$this->getSmarty();
		$tagid = isset ( $_POST ['tagid'] ) ?  $_POST ['tagid']  : "";
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 0;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 30;
		$click_count = isset ( $_POST ['order'] ) ? $_POST ['order'] : "";
		require_once SERVICE . DS . 'IndexService.class.php';
		$indexService = new IndexService( $this->getDB () );
		$order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';
		$where = "";
	
		if (empty($tagid)){
			$where = "";
		}else {
			//根据标签id获取文章ID
			$postId = $this->getPostIdByTagId($tagid);
			$where = "ID in ( ".rtrim($postId, ",").") AND";
		}
			
		$result = $indexService->loadPosts4ListCategory($page, $rows, $order, $where, "load");
		echo json_encode ( $result );
	}

	

    /**
     * 这个方法就是以瀑布布局的方式展示所有这个tag的内容。页面跟category一样
     * 只是在导航条的部分需要写成
     * 首页 > Tag
     * @param $param 参数
     */
    public function showtag($param){
        $smaryt = $this->getSmarty();
        //这里暂进没有想好怎么放
        //这个就是tagid
        $tagId =  $param[0];
        require_once COMMON . DS  . 'Constant.class.php';
        $this->smarty->assign ( 'tags',Constant::getHotTag() );
        require_once SERVICE . DS  . 'TagService.class.php';
        $tagService = new TagService($this->getDB());
        $tagname = "";
        if (empty($tagId)){
            $tagname = "爱博123 标签";
        }else{
            $tag = $tagService->getTagByTagId($tagId);
            $tagname = $tag->tag_name;
        }
        $this->smarty->assign ( 'pagesets', Constant::tagHeader($tagname));
        $this->smarty->assign ( 'tagname',$tagname);
        $this->smarty->assign ( 'crumb',"&gt;<a href='".WEBSITE_URL."tag'>$tag->tag_name</a>");

        $click_count = isset ( $_GET ['order'] ) ? $_GET ['order'] : "";
        //$tagId = isset ( $_GET ['tagid'] ) ?  $_GET ['tagid']  : "";
        $order = empty($click_count)?"order by  post_date desc ":'order by  '.$click_count.' desc';

        $where = "";
        if (empty($tagId)){
            $where = "";
        }else {
            //根据标签id获取文章ID
            $result = $this->getPostIdByTagId($tagId);
            $where = "ID in ( ".rtrim($result, ",").") AND";
        }

        $this->getTemplatePosts($order, $where);
        $this->smarty->display("tag.tpl");
    }


}